class LRUCache {
    constructor(capacity) {
        this.capacity = capacity;
        this.usedNum = 0;
        this.map = new Map();
        this.queue = [];
    }

    has(key) {
        return this.map.has(key);
    }

    get(key) {
        if (this.map.has(key)) {
            let v = this.map.get(key);
            this.queue.splice(this.queue.indexOf(key), 1);
            this.queue.push(key);
            return v;
        }
        return null;
    }

    put(key, value) {
        if (this.map.has(key)) {
            this.map.set(key, value);
            return;
        }
        if (this.usedNum >= this.capacity) {
            let k = this.queue.shift();
            this.map.delete(k);
            this.usedNum -= 1;
        }
        this.queue.push(key);
        this.map.set(key, value);
        this.usedNum += 1;
    }
}

export {
    LRUCache
}
